package com.macro.mall.service.impl;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.macro.mall.mapper.BmsDailyAttendanceRecordMapper;
import com.macro.mall.model.BmsDailyAttendanceRecord;
import com.macro.mall.query.BmsDailyAttendanceRecordQuery;
import com.macro.mall.request.BmsDailyAttendanceRecordParam;
import com.macro.mall.service.BmsDailyAttendanceRecordService;
import com.macro.mall.utils.AdminUtils;

import cn.hutool.core.date.LocalDateTimeUtil;

@Service
public class BmsDailyAttendanceRecordServiceImpl implements BmsDailyAttendanceRecordService {

    @Autowired
    private BmsDailyAttendanceRecordMapper baseMapper;

    @Override
    public List<BmsDailyAttendanceRecord> findList(BmsDailyAttendanceRecordQuery query) {
        Integer pageNum = query.getPageNum();
        Integer pageSize = query.getPageSize();
        if (Objects.nonNull(pageNum) && Objects.nonNull(pageSize)) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<BmsDailyAttendanceRecord> list = baseMapper.findList(query);
        return list;
    }

    @Override
    public int create(BmsDailyAttendanceRecordParam param) {
        BmsDailyAttendanceRecord entity = new BmsDailyAttendanceRecord();
        BeanUtils.copyProperties(param, entity);
        AdminUtils.setCreation(entity);
        return baseMapper.insert(entity);
    }

    @Override
    public int update(BmsDailyAttendanceRecordParam param) {
        BmsDailyAttendanceRecord entity = baseMapper.getById(param.getId());
        BeanUtils.copyProperties(param, entity);
        AdminUtils.setEdition(entity);
        return baseMapper.updateById(entity);
    }

    @Override
    public BmsDailyAttendanceRecord getById(Long id) {
        return baseMapper.getById(id);
    }

    @Override
    public int delete(List<Long> ids) {
        return baseMapper.updateDeleteId(ids);
    }

    @Override
    public void runNextDayAttendanceRecord() {
        LocalDateTime dateTime = LocalDateTimeUtil.now();
        dateTime = dateTime.plusDays(1);
        baseMapper.deleteAttendanceRecord(dateTime);
        baseMapper.insertAttendanceRecord(dateTime);
    }
    
    @Override
    public List<BmsDailyAttendanceRecord> findExportList(BmsDailyAttendanceRecordQuery query) {
        List<BmsDailyAttendanceRecord> list = baseMapper.findList(query);
        return list;
    }

}
